package Control;

import java.util.List;

import objects.Gene;
import objects.Subset;
import objects.Transcript;
import objects.Treatment;


public class DataFetcher extends InitializeData {

	private static DataFetcher instance = null;

	protected DataFetcher() {
	}

	public static DataFetcher getInstance() {
		if (instance == null) {
			instance = new DataFetcher();
		}
		return instance;
	}

	/**
	 * From the supplied gene name, the gene is fetched from the database and
	 * returned to the user.
	 * 
	 * @param geneName
	 * @return
	 */
	public Gene getGene(String geneName) {
		conn = DataConnection.getInstance();
		Gene gene = null;

		try {
			gene = loadGene(geneName);
			loadTreatments(gene);

		} catch (Exception e) {
			handleException(e);
		}

		return gene;
	}

	/**
	 * Loads the transcript information for the gene. This includes the
	 * associated Exons.
	 */

	public void loadTranscriptInformation(Gene gene) {
		try {
			loadTranscripts(gene);

			List<Transcript> transcripts = gene.getTranscripts();

			for (Transcript t : transcripts) {
				loadExons(t);
			}

		} catch (Exception e) {
			handleException(e);
		}
	}

	/**
	 * Given the current gene and the treatment ID, load the alignment
	 * information so that we can graph the subsets.
	 */
	public void loadAlignmentInformation(Gene gene, Integer treatmentID) {
		try {
			Treatment t = gene.getTreatmentByID(treatmentID);
			if (t == null || !t.isEmpty()) {
				return;
			}

			loadSubsets(t);

			List<Subset> subsets = t.getAllSubsets();
			for (Subset s : subsets) {
				if (s.getTreatmentParent() == null)
					s.setTreatmentParent(t);

				if (s.isEmpty())
					loadAlignments(s);
			}
		} catch (Exception e) {
			handleException(e);
		}
	}
}
